/**
 *  Copyright ${date} Matthew Cline
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package factbook;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.zip.ZipException;

/**
 * A library that provides access to the CIA World Factbook.
 * 
 * <p>
 * The quickest way to use this library:
 * <ol>
 * <li>Create a new Factbook object that just uses a connection to the CIA Factbook website:
 * 		
 * 		<p><code>Factbook factbook = new Factbook();</code></p>
 * 	</li>
 * <li>Specific the FIPS code and data field that you want. Note that the FIPS code should be lower-case, while the data field name is case-sensitive.
 * 	   For example, to get the Background information for the United States:
 * 
 * 		<p><code>String result = factbook.get("us","Background");</code></p>
 * </li>
 * </ol>
 * </p>
 * @author Matthew Cline
 *
 */
public class Factbook {
	
	public static final String DEFAULT_URL = "https://www.cia.gov/library/publications/the-world-factbook/";
	
	private FactbookSource source;
	
	/**
	 * Default constructor that uses the CIA World Factbook site as a data source.
	 */
	public Factbook() {
		try {
			source = new FactbookWeb(new URL(DEFAULT_URL));
		} catch (MalformedURLException e) {
			// We should never get here
			System.err.println("The default URL is mis-specified: " + DEFAULT_URL);
			System.exit(1);
		}
	}

	/**
	 * 
	 * @param location The base URL for the site containing the Factbook files, eg. "https://www.cia.gov/library/publications/the-world-factbook/"
	 */
	public Factbook(URL location) {
		source = new FactbookWeb(location);
	}
	
	/**
	 * 
	 * @param file The archive file downloaded from the CIA World Factbook website
	 * @throws ZipException
	 * @throws IOException
	 */
	public Factbook(File file) throws ZipException, IOException {
		source = new FactbookZIP(file);
	}
	
	/**
	 * 
	 * @param fips The <a href="http://en.wikipedia.org/wiki/List_of_FIPS_country_codes">FIPS code</a> of the country for which you want the data.
	 * @param field The data field that you want. I would suggest look at one of the <a href="https://www.cia.gov/library/publications/the-world-factbook/geos/us.html">Factbook pages</a> to see the available options.
	 * @return result The value of the requested data field.
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public String get(String fips, String field) throws MalformedURLException, IOException {
		return source.get(fips, field);
	}
}
